package etxt2db.corpus;

import edu.cmu.minorthird.text.Span;
import edu.cmu.minorthird.text.TextToken;
import edu.cmu.minorthird.text.Token;

public class NoTokenSpan implements Span {
	
	private int loChar;
	private int hiChar;
	private Span documentSpan;
	
	public NoTokenSpan(Span documentSpan, int loChar, int hiChar){
		this.loChar = loChar;
		this.hiChar = hiChar;
		this.documentSpan = documentSpan;
	}

	@Override
	public String asString() {
		return documentSpan.asString().substring(loChar-documentSpan.getLoChar(),hiChar-documentSpan.getLoChar());
	}

	@Override
	public Span charIndexProperSubSpan(int arg0, int arg1) {
		return new NoTokenSpan(documentSpan,loChar+arg0,loChar+arg1);
	}

	@Override
	public Span charIndexSubSpan(int arg0, int arg1) {
		return new NoTokenSpan(documentSpan,loChar+arg0,loChar+arg1);
	}

	@Override
	public boolean contains(Span arg0) {
		if(!arg0.getDocumentId().equals(getDocumentId()))
			return false;
		return arg0.getLoChar()>this.getLoChar() && arg0.getHiChar()<this.getHiChar();
	}

	@Override
	public Span documentSpan() {
		return documentSpan;
	}

	@Override
	public int documentSpanStartIndex() {
		return 0;
	}

	@Override
	public int getCharOffset() {
		return 0;
	}

	@Override
	public String getDocumentContents() {
		return documentSpan.asString();
	}

	@Override
	public String getDocumentGroupId() {
		return documentSpan.getDocumentGroupId();
	}

	@Override
	public String getDocumentId() {
		return documentSpan.getDocumentId();
	}

	@Override
	public int getHiChar() {
		return hiChar;
	}

	@Override
	public Span getLeftBoundary() {
		return this;
	}

	@Override
	public int getLoChar() {
		return loChar;
	}

	@Override
	public int getLoTextToken() {
		return 0;
	}

	@Override
	public Span getRightBoundary() {
		return this;
	}

	@Override
	public TextToken getTextToken(int arg0) {
		return null;
	}

	@Override
	public Token getToken(int arg0) {
		return null;
	}

	@Override
	public boolean overlaps(Span other) {
		if(!other.getDocumentId().equals(getDocumentId()))
			return false;
		int myStart=documentSpanStartIndex();
		int otherStart=other.documentSpanStartIndex();
		int myEnd=documentSpanStartIndex()+size();
		int otherEnd=other.documentSpanStartIndex()+other.size();
		return(myStart<=otherStart&&myEnd>=otherStart // [ ... ( ... ] - partial containment 1
				||myStart<=otherEnd&&myEnd>=otherEnd // [ ... ) ... ] - partial containment 2
		||other.contains(this) // ( .. [ ... ] ... ) - containment 
		);
	}

	@Override
	public void setCharOffset(int arg0) {
	}

	@Override
	public int size() {
		return hiChar-loChar;
	}

	@Override
	public Span subSpan(int arg0, int arg1) {
		return null;
	}

	@Override
	public int compareTo(Span other) {
		int cmp1=getDocumentId().compareTo(other.getDocumentId());
		if(cmp1!=0)
			return cmp1;
		int cmp2=documentSpanStartIndex()-other.documentSpanStartIndex();
		if(cmp2!=0)
			return cmp2;
		int cmp3=size()-other.size();
		if(cmp3!=0)
			return cmp3;
		return 0;
	}
}
